package com.example.streams;

import com.example.beanutils.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class StreamTest4 {
    public static void main(String[] args) {
        /**
         * 统计同名用户的年纪合
         */
        User user1 = new User("王五", 1, "男");
        User user2 = new User("王五", 99, "男");
        User user3 = new User("王五", 2, "男");
        User user4 = new User("张三", 2, "男");
        User user5 = new User("张三", 3, "男");
        User user6 = new User("", 2, "男");
        User user7 = new User(null, 2, "男");
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        list.add(user6);

        Map<String, Integer> map = list.stream().filter(f -> f.getName() != null && f.getName() != "")
                .collect(Collectors.groupingBy(k ->k.getName(),
                Collectors.collectingAndThen(Collectors.toList(),
                        x -> x.stream().map(User::getAge).reduce(0, Integer::sum))));
        System.out.println(map);
    }
}
